package stark.app.function.catdog;

import android.graphics.Color;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.catdog.lib.AnimalSound;

import org.jetbrains.annotations.NotNull;

import stark.app.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;

/**
 * Author：daizhongbin on 2022/8/31
 * Email：1027154338@qq.com
 * Desc:
 */
public class CatDogAdapter extends StkProviderMultiAdapter<AnimalSound> {

    public static final int TYPE_CAT = 1;
    public static final int TYPE_DOG = 2;

    private int mType = TYPE_CAT;
    private int mPlayingPos = -1;

    public CatDogAdapter(int type) {
        super(2);
        if (type != TYPE_CAT && type != TYPE_DOG) {
            type = TYPE_CAT;
        }
        mType = type;
        addItemProvider(new MyItemProvider());
    }

    public void setPlayingPos(int playingPos) {
        this.mPlayingPos = playingPos;
        notifyDataSetChanged();
    }

    private class MyItemProvider extends BaseItemProvider<AnimalSound> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_cat_dog;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, AnimalSound animalSound) {
            int pos = holder.getAdapterPosition();
            boolean isPlaying = pos == mPlayingPos;

            ImageView ivBg = holder.getView(R.id.ivBg);
            ImageView ivLaba = holder.getView(R.id.ivLaba);
            TextView tvName = holder.getView(R.id.tvName);
            tvName.setText(animalSound.soundName);
            if (mType == TYPE_CAT) {
                ivBg.setImageResource(R.drawable.ic_cat_item_bg);
                ivLaba.setImageResource(isPlaying ? R.drawable.ic_pause_cat : R.drawable.ic_laba_cat);
                tvName.setTextColor(Color.parseColor("#fffeb40e"));

            } else {
                ivBg.setImageResource(R.drawable.ic_dog_item_bg);
                ivLaba.setImageResource(isPlaying ? R.drawable.ic_pause_dog : R.drawable.ic_laba_dog);
                tvName.setTextColor(Color.parseColor("#9d6d45"));

            }

        }
    }

}
